package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.mybatis.SSRemindDrugsDAO;
import com.trkj.pojos.DrugstorageReminddrug;

/**
 * 药库到期药品service
 * @author Administrator
 *
 */
@Controller
@Transactional
public class SSExpiryDrugsService {
	@Autowired
	private SSRemindDrugsDAO ssRemindDrugsDao;
	/**
	 * 获取所有的药库提醒使用药品
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map getAllSSRemindDrugs(int page ,int rows) {
		Page<DrugstorageReminddrug> p =PageHelper.startPage(page, rows,true) ;
		List<DrugstorageReminddrug> lists = ssRemindDrugsDao.getAllSSRemindDrugs();
		Map map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", this.jsonJieXi(lists));
		return map;
	}
	/**
	 * 按药品名称查询药库提醒使用药品
	 * @param page
	 * @param rows
	 * @param map
	 * @return
	 */
	public Map getSSRemindDrugByName(int page,int rows,Map map) {
		Page<DrugstorageReminddrug> p = PageHelper.startPage(page, rows, true);
		List<DrugstorageReminddrug> lists = ssRemindDrugsDao.getSSRemindDrugByName(map);
		Map map1 = new HashMap<>();
		map1.put("total", p.getTotal());
		map1.put("rows", this.jsonJieXi(lists));
		return map1;
	}
	/**
	 * 按条件查询药库提醒使用药品
	 * @param page
	 * @param rows
	 * @param map
	 * @return
	 */
	public Map getSSExpiryDrugBySome(int page,int rows,Map map) {
		Page<DrugstorageReminddrug> p = PageHelper.startPage(page, rows, true);
		List<DrugstorageReminddrug> list = ssRemindDrugsDao.getSSRemindDrugsBySome(map);
		Map map1 = new HashMap<>();
		map1.put("total", p.getTotal());
		map1.put("rows", this.jsonJieXi(list));
		return map1;
	}
	/**
	 * 获取药库拥有的药品详情map
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map getSSHasDrugs(int page,int rows) {
		return null;
		
	}
	/**
	 * 解析字符串
	 * @param list
	 * @return
	 */
	public List<Map> jsonJieXi(List<DrugstorageReminddrug> list){
		List<Map> lists = new ArrayList<>();
		if(!list.isEmpty()) {
			String s = JSON.toJSONString(list);
			JSONArray arr = JSON.parseArray(s);
			for(int i = 0;i<list.size();i++) {
				JSONObject obj = arr.getJSONObject(i);
				Map map = new HashMap<>();
				map.put("drugstorageRemindId", obj.getLong("drugstorageRemindId"));
				map.put("medcineCost", obj.getBigDecimal("medcineCost"));
				map.put("medcineNumber", obj.getBigDecimal("medcineNumber"));
				map.put("remaindate", obj.getDate("remaindate"));
				map.put("remindNote", obj.getString("remindNote"));
				JSONObject medcineDetail = obj.getJSONObject("medcineDetail");
				map.put("medcineId", medcineDetail.getLong("medcineId"));
				map.put("medcineName", medcineDetail.getString("medcineName"));
				map.put("medcinePrice", medcineDetail.getBigDecimal("medcinePrice"));
				JSONObject staff1 = obj.getJSONObject("staff1");
				map.put("staffId", staff1.getLong("staffId"));
				map.put("staffname", staff1.getString("staffname"));
				lists.add(map);
			}
		}
		return lists;
	}
}
